package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.core.page.PagePlus;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.bo.ZsyyCourseAddBo;
import com.ruoyi.system.bo.ZsyyCourseQueryBo;
import com.ruoyi.system.bo.ZsyyCourseEditBo;
import com.ruoyi.system.domain.ZsyyCourse;
import com.ruoyi.system.mapper.ZsyyCourseMapper;
import com.ruoyi.system.vo.ZsyyCourseVo;
import com.ruoyi.system.service.IZsyyCourseService;
import com.ruoyi.system.mapper.ZsyyCourseMapper;
import java.util.List;
import java.util.Map;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
/**
 * 产品分类Service业务层处理
 *
 * @author wangghua
 * @date 2021-07-14
 */
@Service
public class ZsyyCourseServiceImpl extends ServicePlusImpl<ZsyyCourseMapper, ZsyyCourse> implements IZsyyCourseService {
    @Autowired
    private ZsyyCourseMapper iZsyyCourseMapper;

    @Override
    public ZsyyCourseVo queryById(Long id){
        return getVoById(id, ZsyyCourseVo.class);
    }

    @Override
    public TableDataInfo<ZsyyCourseVo> queryPageList(ZsyyCourseQueryBo bo) {
		Page<ZsyyCourseVo> page = iZsyyCourseMapper.queryPageList(PageUtils.buildPage(),bo);
		return PageUtils.buildDataInfo(page);
    }

    @Override
    public List<ZsyyCourseVo> queryList(ZsyyCourseQueryBo bo) {
        return listVo(buildQueryWrapper(bo), ZsyyCourseVo.class);
    }

    private LambdaQueryWrapper<ZsyyCourse> buildQueryWrapper(ZsyyCourseQueryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZsyyCourse> lqw = Wrappers.lambdaQuery();
        lqw.like(StrUtil.isNotBlank(bo.getTitle()), ZsyyCourse::getTitle, bo.getTitle());
        lqw.eq(bo.getParentId() != null, ZsyyCourse::getParentId, bo.getParentId());
        lqw.eq(bo.getGrade() != null, ZsyyCourse::getGrade, bo.getGrade());
		lqw.orderByAsc(ZsyyCourse::getParentId);
        return lqw;
    }

    @Override
    public Boolean insertByAddBo(ZsyyCourseAddBo bo) {
        ZsyyCourse add = BeanUtil.toBean(bo, ZsyyCourse.class);
        validEntityBeforeSave(add);
        return save(add);
    }

    @Override
    public Boolean updateByEditBo(ZsyyCourseEditBo bo) {
        ZsyyCourse update = BeanUtil.toBean(bo, ZsyyCourse.class);
        validEntityBeforeSave(update);
        return updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(ZsyyCourse entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return removeByIds(ids);
    }
}
